package com.shuai.shuaisecondhand_springboot.mapper;

import com.shuai.shuaisecondhand_springboot.entity.Goods;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface GoodsMapper {

    //sql->查询
    @Select("SELECT * FROM goods")
    List<Goods> findAll();

    //sql->根据Uid查询数据
    @Select("SELECT * FROM goods WHERE Uid=#{Uid} AND (IFNULL(status,'') LIKE concat('%', #{status}, '%')) AND (IFNULL(name,'') LIKE concat('%', #{name}, '%')) ORDER BY time DESC")
    List<Goods> getUidGoods(@Param("Uid") String Uid,@Param("status") String status,@Param("name") String name);

    //sql->根据id查询所有数据
    @Select("SELECT * FROM goods WHERE id=#{Gid}")
    Goods findOne(@Param("Gid") Integer Gid);

    //sql->插入(只能由用户用户去添加商品数据)
    @Insert("INSERT INTO goods(uid,`name`,buy_price,sell_price,photo,`describe`,sell_username) VALUES (#{uid},#{name},#{buyPrice},#{sellPrice},#{photo},#{describe},#{sellUsername})")
    Boolean insertGoods(Goods goods);

    //sql->修改商品状态(用户用户修改商品状态)
    @Update("UPDATE goods SET status=#{status} WHERE id=#{id}")
    Boolean updateGoodsStatus(@Param("id") String id,@Param("status") String status);

    //sql->修改(管理员修改商品信息)
    /*@Update("UPDATE goods SET Gname=#{Gname},Gbuyprice=#{Gbuyprice},Gsellprice=#{Gsellprice},Gphoto=#{Gphoto},Gdescribe=#{Gdescribe} WHERE Gid=#{Gid}")*/
    Boolean updateGoods(Goods goods);

    //sql->修改(用户用户修改商品信息)
    Boolean updateUserGoods(Goods goods);

    //sql->删除
    @Delete("DELETE FROM goods WHERE id=#{id}")
    Integer deleteGoods(long id);

    //mybatis的xml方式用sql->批量删除
    Integer deleteGoodsBatch(List<Integer> ids);

    //sql->用户分页查询
    @Select("SELECT *  FROM goods WHERE (IFNULL(name,'') LIKE concat('%', #{name}, '%')) AND audit='1' ORDER BY time DESC limit #{pageNum}, #{pageSize} ")
    List<Goods> getGoodsPage(@Param("pageNum") Integer pageNum,@Param("pageSize") Integer pageSize,@Param("name") String name);

    //sql->查询Uid所有数据量
    @Select("SELECT count(*) FROM goods WHERE Uid=#{Uid} AND (IFNULL(status,'') LIKE concat('%', #{status}, '%')) AND (IFNULL(name,'') LIKE concat('%', #{name}, '%'))")
    int getSidTotal(@Param("Uid") String Uid,@Param("status") String status,@Param("name") String name);

    //sql->用户查询所有数据量
    @Select("SELECT count(*) FROM goods WHERE (IFNULL(name,'') LIKE concat('%', #{name}, '%')) AND audit='1'")
    int getTotal(@Param("name") String name);

    //sql->查询商品总数
    @Select("SELECT COUNT(*) FROM goods")
    int getAllTotal();

    //sql->管理员分页查询
    @Select("SELECT *  FROM goods WHERE (IFNULL(name,'') LIKE concat('%', #{name}, '%')) ORDER BY time DESC limit #{pageNum}, #{pageSize} ")
    List<Goods> getAdminGoodsPage(@Param("pageNum") Integer pageNum,@Param("pageSize") Integer pageSize,@Param("name") String name);

    //sql->管理员查询所有数据量
    @Select("SELECT count(*) FROM goods WHERE (IFNULL(name,'') LIKE concat('%', #{name}, '%'))")
    int getAdminTotal(@Param("name") String name);
}
